package com.lsa.letcode.the58.lengthLastWord;

public class Solution {
	enum State{
    	InWord,
    	InSpace
    };
    public int lengthOfLastWord(String s) {
    	State state = State.InSpace;
    	
    	int start = -1;
    	int end = -1;
    	
    	for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			switch (state) {
			case InSpace:
				if (!Character.isWhitespace(c)) {
					start = i;
					end = i;
					state = State.InWord;
				}
				break;
			case InWord:
				if (Character.isWhitespace(c)) {
					state = State.InSpace;
				}
				else {
					end = i;
				}
				break;
			}
		}
    	
    	return start > -1? end - start + 1 : 0;
    }
}